#include <string.h>
#include <stdio.h>
#include "filehandler.h"

/**	@fn		FILE* fileopen(char strFile[])
	@brief	opens a file given the path as strFile[]
	@param	strFile		the path of the to be opened file
	@param  mode        the mode with which to open the file
	@return	file pointer on succes, NULL on fail
	*/
FILE *fileopen(char strFile[], int mode){
    if(DEBUG){printf("\nfileopen()");}
    FILE * fTarget;
    switch (mode)
    {
        case 1:
            //write only
            if((fTarget = fopen(strFile, "w"))){
                if(DEBUG){printf("\nfile %s open at %X", strFile, (int)fTarget);}
                return(fTarget);
            }else{
                if(DEBUG){printf("\nfile cannot open %s", strFile);}
                return(0);
            }
            break;
    	case 2:
            //read only
    	default:
            if((fTarget = fopen(strFile, "r"))){
                if(DEBUG){printf("\nfile %s open at %X", strFile, (int)fTarget);}
                return(fTarget);
            }else{
                if(DEBUG){printf("\nfile cannot open %s", strFile);}
                return(0);
            }
    		break;
    }
}

/**	@fn		void fileclose(FILE * fTarget)
	@brief	closes a opened file
	@param	fTarget	file pointer of open file
	@return void
	*/
void fileclose(FILE * fTarget){
    if(DEBUG){printf("\nfileclose()");}
     if(fTarget != 0){
          if(DEBUG){printf("\nfile close at %X", (int)fTarget);}
          fTarget = 0;
     }else{
          if(DEBUG){printf("\nfile NULL pointer at %X", (int)fTarget);}
     }
}
